import pandas as pd
from openai import OpenAI
from sklearn.metrics import accuracy_score
import ollama
from pandasai.llm.local_llm import LocalLLM
from pandasai import SmartDataframe
import numpy as np
import pandas as pd
from warnings import simplefilter
simplefilter(action="ignore", category=pd.errors.PerformanceWarning)



def query_to_llm(filename,model,query):
    df = SmartDataframe(filename, config={"llm": model})
    result = df.chat(query)
    return result

def truth_value(df,query):
    result = eval(query)
    return result


if __name__ == '__main__':
    filename = 'data/export_179004_0.csv'
    df=pd.read_csv(filename)
    model = LocalLLM(api_base="http://localhost:11434/v1", model="gemma2:27b")
    result_llm = query_to_llm(filename,model,'which Town Name has the highest number of accidents?')
    query = 'df.groupby(\'Town Name\').size().idxmax()'
    result = truth_value(df,query)
    if result in result_llm:
        print('The result is correct')
    else:
        print('The result is incorrect')
        print(result_llm)
        print(result)